package interfaces.options;

import fileHandling.language.Lang;
import interfaces.GUISource;
import interfaces.language.LanguageLabel;
import interfaces.superWidgets.StaticContent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.util.Color;

public class OptionsContainer extends StaticContent {
	
	public static final Color ROW_COLOR = Color.DARK_GRAY;
	
	private ImageFont font;
	private int optionHeight;
	private List<OptionWidget> optionList;
	private HashMap<OptionWidget, StaticContent> widgetContentMap;
	private List<TextEditorOptionWidget> textOptions;
	
	public OptionsContainer(int width, int height, int x, int y) {
		super(width, height, x, y);
		
		font = GUISource.middleFont;
		optionHeight = getOptionHeight();
		
		optionList = new ArrayList<OptionWidget>();
		widgetContentMap = new HashMap<OptionWidget, StaticContent>();
		textOptions = new ArrayList<TextEditorOptionWidget>();
	}
	
	public static int getOptionHeight() {
		return GUISource.middleFont.getHeight() / 3 * 4;
	}
	
	public void addOption(OptionWidget option) {
		StaticContent content = new StaticContent(width, optionHeight, 0, height - (optionHeight * (optionList.size() + 1)));
		
		if(optionList.size() % 2 == 1) 
			content.getAppearance().add(new PlainBackground(ROW_COLOR));
		
		LanguageLabel optionLabel = new OptionWidgetLabel(option, font);
		optionLabel.setSize(width / 2, optionHeight);
		optionLabel.setXY(0, 0);
		
		option.setSize(width / 2, optionHeight);
		option.setXY(width - option.getWidth(), 0);
		
		content.addWidget(optionLabel, option);
		addWidget(content);
		
		widgetContentMap.put(option, content);
		optionList.add(option);
		if(option instanceof TextEditorOptionWidget) 
			textOptions.add((TextEditorOptionWidget)option);
		
		layout();
	}
	
	public List<OptionWidget> getOptionList() { return optionList; }
	
	public TextInputError checkTextInput() {
		for(TextEditorOptionWidget t : textOptions) {
			String text = t.checkInput();
			if(text != null) return new TextInputError(t, text);
		}
		return null;
	}
	
	public String getValueOf(Lang description) {
		for(OptionWidget option : optionList) {
			if(option.getDescription().equals(description)) return option.getValue();
		}
		return null;
	}
	
	public void setToHigh() {
		for(OptionWidget option : optionList) {
			option.setToHigh();
		}
	}
	
	public void setToMedium() {
		for(OptionWidget option : optionList) {
			option.setToMedium();
		}
	}
	
	public void setToLow() {
		for(OptionWidget option : optionList) {
			option.setToLow();
		}
	}
	
	public boolean hasOption(OptionWidget option) { return widgetContentMap.containsKey(option); }
	
	public void removeOption(OptionWidget optionWidget) {
		StaticContent content = widgetContentMap.get(optionWidget);
		if(content == null) return;
		
		removeWidget(content);
		optionList.remove(optionWidget);
		widgetContentMap.remove(optionWidget);
		
		layout();
	}
}